﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Zhuang.Utility.WinForm.Event
{
    public class ControlsEventHelper
    {
        /// <summary>
        /// 限制文本框只能输入数字及小数点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void KeyPress_NumberInputOnly(object sender, KeyPressEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            try
            {
                int kc = (int)e.KeyChar;
                if ((kc < 48 || kc > 57) && kc != 8)
                    e.Handled = true;

                if (kc == 46)                       //小数点
                {
                    if (tb.Text.Length <= 0)
                        e.Handled = true;           //小数点不能在第一位
                    else
                    {
                        float f;
                        float oldf;
                        bool b1 = false, b2 = false;
                        b1 = float.TryParse(tb.Text, out oldf);
                        b2 = float.TryParse(tb.Text + e.KeyChar.ToString(), out f);
                        if (b2 == false)
                        {
                            if (b1 == true)
                                e.Handled = true;
                            else
                                e.Handled = false;
                        }
                        else
                        {
                            e.Handled = false;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// 限制文本框只能输入两位小数的数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void KeyPress_2DecimalPointInputOnly(object sender, KeyPressEventArgs e)
        {
             TextBox tb=(TextBox)sender;

            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 46)
            {
                e.Handled = true;
            }
            if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back)   
            {   
                e.Handled = false;       //让操作生效   
                int j = 0;   
                int k = 0;   
                bool flag = false;
                if (tb.Text.Length == 0)   
                {   
                    if (e.KeyChar == '.')   
                    {   
                        e.Handled = true;             //让操作失效   
                    }   
                }
                for (int i = 0; i < tb.Text.Length; i++)   
                {
                    if (tb.Text[i] == '.')   
                    {   
                        j++;   
                        flag = true;   
                    }   
                    if (flag)   
                    {
                        if (char.IsNumber(tb.Text[i]) && e.KeyChar != (char)Keys.Back)   
                        {   
                            k++;   
                        }   
                    }   
                    if (j >= 1)   
                    {   
                        if (e.KeyChar == '.')   
                        {   
                            e.Handled = true;             //让操作失效   
                        }   
                    }   
                    if (k == 2)   
                    {
                        if (char.IsNumber(tb.Text[i]) && e.KeyChar != (char)Keys.Back)   
                        {
                            if (tb.Text.Length - tb.SelectionStart < 3)   
                            {
                                if (tb.SelectedText != tb.Text)   
                                {   
                                    e.Handled = true;             ////让操作失效   
                                }   
                            }   
                        }    
                    }   
                }   
            }   
            else  
            {   
                e.Handled = true;   
            }    
        }
       
    
    }
}
